#coding=utf-8
from uliweb.core.SimpleFrame import expose, url_for
from uliweb.contrib.auth.views import login, register, logout
from uliweb import decorators
from uliweb.i18n import ugettext_lazy as _

def login():
    from uliweb.contrib.auth import authenticate, login
    from forms import LoginForm
    from uliweb.form import Submit, Tag
    import urllib

    LoginForm.form_buttons = [Submit(value=_('Login'), _class="button")]

    form = LoginForm()

    if request.method == 'GET':
        form.next.data = request.GET.get('next', request.referrer or '/')
        return {'form':form, 'msg':''}
    if request.method == 'POST':
        flag = form.validate(request.params)
        if flag:
            f, d = authenticate(username=form.username.data, password=form.password.data)
            if f:
                request.session.remember = form.rememberme.data
                login(form.username.data)
                next = urllib.unquote(request.POST.get('next', '/'))
                return redirect(next)
            else:
                data = d
        m = form.errors.get('_', '') or _('Login failed!')
        return {'form':form, 'msg':str(m)}

@expose('/resign')
def resign():
    from uliweb.contrib.auth import logout
    logout()
    return redirect(url_for('login', next=request.referrer or '/'))

@decorators.require_login
@expose('/settings')
def admin():
    from uliweb.contrib.auth.forms import ChangePasswordForm
    from forms import ChangeInfoForm
    import os.path
    
    ChangePasswordForm.fieldset = False
    ChangeInfoForm.fieldset = False
    changepasswordform = ChangePasswordForm()
    changeinfoform = ChangeInfoForm()
    changeinfoform.email.data = request.user.email
    changeinfoform.image.data = request.user.image
    if request.method == 'GET':
        return {'changepasswordform':changepasswordform, 'changeinfoform':changeinfoform, 'message1':'', 'message2':''}
    if request.method == 'POST':
        if request.POST.get('action') == 'changepassword':
            flag = changepasswordform.validate(request.POST, request=request)
            if flag:
                request.user.set_password(changepasswordform.password.data)
                return redirect(request.path)
            else:
                message = "There was something wrong! Please fix them."
                return {'changepasswordform':changepasswordform, 
                    'changeinfoform':changeinfoform, 'message1':message}
        if request.POST.get('action') == 'changeinfo':
            flag = changeinfoform.validate(request.POST, request.FILES)
            if flag:
                #saving image
                if changeinfoform.image.data:
                    #using save_image_field
                    from uliweb.contrib.upload import save_image_field
                    filename = request.user.username + os.path.splitext(changeinfoform.image.data.filename)[1]
                    filename = os.path.join('portrait', filename)
                    save_image_field(changeinfoform.image, replace=True, resize_to=(50, 50), filename=filename)
#using save_file_field
#                    from uliweb.contrib.upload import save_file_field
#                    save_file_field(changeinfoform.image, replace=True, subfolder='portrait')
#old usage
#                    changeinfoform.image.data.filename = save_file(changeinfoform.image.data.filename, 
#                        changeinfoform.image.data.file, replace=True, subfolder='portrait')
#                    profile.image = changeinfoform.image.data.filename
                request.user.image = changeinfoform.image.data.filename
                request.user.email = changeinfoform.email.data
                request.user.save()
                return redirect(request.path)
            else:
                message = "There was something wrong! Please fix them."
                return {'changepasswordform':changepasswordform, 
                    'changeinfoform':changeinfoform, 'message2':message}
            
    
